package com.json.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;  
import com.json.service.JsonService;
import com.mytest.Connect;
import com.json.service.*;
public class JsonServlet extends HttpServlet {
	private JsonService service;
	private Connect conn = new Connect();
	/**
	 * Constructor of the object.
	 */
	public JsonServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
	
		String jsonString = "hello world";
		String username= "debug";
		String password= "debug";
		String usercity= "debug";
		
		boolean result ;

		String user = request.getParameter("user");
		if(user != null)
		{
			username = conn.get_username(user);
			out.println(username);
		}
		String pass = request.getParameter("pass");
		if(pass!=null){
			password = conn.get_password(pass);
			out.println(password);
		}
			
		
		String user_register = request.getParameter("user_register");
		String pass_register = request.getParameter("pass_register");
		String user_city = request.getParameter("user_city");
		if(user_register !=null && pass_register!=null && user_city != null){
			result = conn.get_userregister(user_register,pass_register,user_city);
			out.println(result);
			}
		if(user_city != null){
			usercity = conn.get_usercity(user_city);
			out.println(usercity);
		}
		
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		service = new JsonService();
	}

}
